package com.zhuojy.equity.mall.admin.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhuojy.equity.mall.admin.common.entity.bo.HolidayBO;
import com.zhuojy.equity.mall.admin.common.entity.po.Balance;
import com.zhuojy.equity.mall.admin.common.entity.po.Rules;
import com.zhuojy.equity.mall.admin.common.entity.po.THolidayPo;
import com.zhuojy.equity.mall.admin.common.entity.TUser;
import com.zhuojy.equity.mall.admin.common.entity.vo.HolidayVO;
import com.zhuojy.equity.mall.admin.common.support.JsonResult;
import com.zhuojy.equity.mall.admin.dal.mapper.THolidayMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class THolidayService extends ServiceImpl<THolidayMapper, THolidayPo> {
    //注入余额接口
    @Autowired
    private BalanceService balanceService;
    //注入规则接口
    @Autowired
    private RulesService rulesService;

    /**
     * 测试查询
     *
     * @param tHoliday
     * @return
     */
    public List<THolidayPo> findtholiday(THolidayPo tHoliday) {
        return this.baseMapper.selectList(null);
    }

    /**
     * 假期类型条查+分页
     *
     * @param tHoliday
     * @param pageNum
     * @param pageSize
     * @return
     */
    public JsonResult findholidayPagination(THolidayPo tHoliday, Integer pageNum, Integer pageSize) {
        Page<THolidayPo> page = new Page<>(pageNum, pageSize);
        IPage<THolidayPo> tHolidayPage = this.baseMapper.findholidayPagination(tHoliday, page);
        return JsonResult.success(tHolidayPage.getRecords(), tHolidayPage.getTotal());
    }

    /**
     * 假期类型表删除(单删)
     *
     * @param id
     */
    public void deleteHoliday(Integer id) {
        this.baseMapper.deleteHoliday(id);
    }

    /**
     * 假期类型新增
     *
     * @param holidayBO
     */
    public void addHoliday(HolidayBO holidayBO) {
        Rules rules = new Rules();
        //使用BeanUtils.copyProperties()方法将holidayBO对象的属性复制到rules对象中
        BeanUtils.copyProperties(holidayBO, rules);
        //调用rulesService.save()方法将rules对象保存到数据库中。
        rulesService.save(rules);

        Balance balance = new Balance();
        //使用BeanUtils.copyProperties()方法将holidayBO对象的属性复制到balance对象中
        BeanUtils.copyProperties(holidayBO, balance);
        //调用balanceService.save()方法将balance对象保存到数据库中。
        balanceService.save(balance);

        //创建了一个新的THoliday对象tHoliday1，并设置了其创建时间
        THolidayPo tHoliday1 = new THolidayPo();
        tHoliday1.setCreateTime(new Date());
        //使用BeanUtils.copyProperties()方法将holidayBO对象的属性复制到tHoliday1对象中
        BeanUtils.copyProperties(holidayBO, tHoliday1);
        //取到主表中的与副表关联的id，转换为integer
        tHoliday1.setRulesId(rules.getId().intValue());
        tHoliday1.setSettingId(balance.getId().intValue());
        //save()方法将tHoliday1对象保存到数据库中
        save(tHoliday1);
//        System.out.println(tHoliday1);
//        System.out.println(rules);
//        System.out.println(balance);

    }

    /**
     * 假期类型回显
     *
     * @param id
     * @return
     */
    public HolidayVO findholidayId(Integer id) {
        //通过id查询对应的信息
        THolidayPo tHoliday = this.baseMapper.selectById(id);
        Balance balance = balanceService.getById(tHoliday.getSettingId());
        Rules rules = rulesService.getById(tHoliday.getRulesId());
        //新建vo实体类m
        HolidayVO holidayVO = new HolidayVO();
        //通过BeanUtils进行拷贝
        BeanUtils.copyProperties(tHoliday, holidayVO);
        BeanUtils.copyProperties(balance, holidayVO);
        BeanUtils.copyProperties(rules, holidayVO);
        System.out.println(holidayVO);
        return holidayVO;
    }

    /**
     * 假期类型表修改
     *
     * @param
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateHoliday(HolidayBO holidayBO) {
        THolidayPo tHoliday = new THolidayPo();
        Balance balance = new Balance();
        Rules rules = new Rules();

        BeanUtils.copyProperties(holidayBO, tHoliday);
        BeanUtils.copyProperties(holidayBO, balance);
        balance.setId(tHoliday.getSettingId().longValue());
        BeanUtils.copyProperties(holidayBO, rules);
        rules.setId(tHoliday.getRulesId().longValue());

        updateById(tHoliday);
        rulesService.updateById(rules);
        balanceService.updateById(balance);

//        update(Wrappers.lambdaUpdate(THoliday.class).eq(THoliday::getId,1).set(THoliday::getUpdateTime,new Date()));
    }

    /**
     * 假期表新增负责人动态下拉列表
     * @return
     */
    public List<TUser> findOptions() {
        return this.baseMapper.findOptions();
    }
}
